-- NewTeamMemberInfoPanel
-- 对应 Node_player_team

local HeadRender = require("app.widget.renders.HeadRender")

local ClassRef = app.mvc.createPanelClass(...)

local string_split = string.split

function ClassRef.create(node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.NewTeamMemberInfoNode))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init()
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	self._baseInfo = nil -- 玩家基础信息，跟self._data同时最多只能有一个有数据
	self._robotInfo = nil -- 机器人信息，跟self._data同时最多只能有一个有数据
	self._partnerInfo = nil -- 随从信息，跟self._data同时最多只能有一个有数据

	self._head = HeadRender.create(self._ui["Panel/Panel_withData/head"], 3)
	self._head:onClick(function(sender)
		if self._data then -- 真实玩家数据？
			if not self._data:isMe() then -- 点击别人头像
				local size = sender:getContentSize()
				local pos = sender:convertToWorldSpace(cc.p(size.width, size.height/2))
				UIHandler:showHeadMenu({online=true, attr=self._data.userBaseInfo, pos=pos})
			end
		elseif self._partnerInfo then -- 随从数据？
			local size = sender:getContentSize()
			local pos = sender:convertToWorldSpace(cc.p(size.width, size.height/2))
			executeFunc(self._onPanelHandler, "MemberInfoPanel_partnerOp", pos, self._partnerInfo:getId())
		elseif self._robotInfo then -- 机器人数据
			local size = sender:getContentSize()
			local pos = sender:convertToWorldSpace(cc.p(size.width, size.height/2))
			executeFunc(self._onPanelHandler, "MemberInfoPanel_robotOp", pos, self._robotInfo:getBaseInfo())
		end
	end)

	local function inviteClickFunc( ... )
		if self._isOnBattlePartner then
			app:sendMsg("PvpEliteController", "openEliteMatchDuelTeamView")
			return
		end
		executeFunc(self._onPanelHandler, "MemberInfoPanel_openInvitePanel") -- 弹开邀请panel
	end
	self._ui["Panel/Button"]:addClickEventListener(function()
			inviteClickFunc()
		end)

	self._dataPanel = self._ui["Panel/Panel_withData"]
	
	self._name = self._ui["Panel/Panel_withData/name"]
	self._class = self._ui["Panel/Panel_withData/class"]
	self._lv_num = self._ui["Panel/Panel_withData/lv_tlt"]
	self._combat_icon = self._ui["Panel/Panel_withData/combat_icon"]
	self._combat_num = self._ui["Panel/Panel_withData/combat_num"]

	self._isLeaderIcon = self._ui["Panel/Panel_withData/icon_leader"]
	self._isReadyIcon = self._ui["Panel/Panel_withData/icon_ok"]
	self._unReadyIcon = self._ui["Panel/Panel_withData/icon_no"]
	self._forbiddenIcon = self._ui["Panel/Panel_withData/Image_Forbidden"]

	self._partnerIcon = self._ui["Panel/Panel_withData/partner"]

	self._leaderTips = self._ui["Panel/Panel_withData/leader_tips"]
	self._leaderTips:setVisible( false )
	self._leaderState = self._ui["Panel/Panel_withData/leader_tips/text"]

	self._followIcon = self._ui["Panel/Panel_withData/icon_follow"]
end

-- data = TeamMemberInfoVo
function ClassRef:refresh()
	self._partnerInfo = nil
	self._robotInfo = nil
	self._baseInfo = nil
	if self._data then -- 有目标队伍
		self._ui["Panel/Panel_withData"]:setVisible( true )
		self._ui["Panel/tips"]:setVisible( false )
		self._ui["Panel/Button"]:setVisible( false )
		self._ui["Panel/stop"]:setVisible( false )
		self._partnerIcon:setVisible( false )
		self._isReadyIcon:setVisible( false ) -- 默认都不显示
		self._unReadyIcon:setVisible( false ) -- 默认都不显示
		self._forbiddenIcon:setVisible( false )
		self._followIcon:setVisible(not self._data:isLeader() and self._data:isWithTeam())

		self._head:setData( self._data.userBaseInfo )
		self._head:setQualityIcon()
		self._name:setString( self._data:getName() )

		self._class:setString(self._data:getDuty())

		self._lv_num:setString( string.format( L("lua_code_text_212"), self._data:getLv() ) )
		self:_refreshCombat( self._data:getCapacity() )
	else
		self._ui["Panel/Panel_withData"]:setVisible( false )
		self._ui["Panel/tips"]:setVisible( true )
		self._ui["Panel/Button"]:setVisible( true )
		self._ui["Panel/stop"]:setVisible( false )
	end
end

-- baseInfo = UserBaseInfo 数据结构
function ClassRef:setBaseInfo( baseInfo )
	self._data = nil
	self._partnerInfo = nil
	self._robotInfo = nil
	self._baseInfo = baseInfo
	if self._baseInfo then
		self._ui["Panel/Panel_withData"]:setVisible( true )
		self._ui["Panel/tips"]:setVisible( false )
		self._ui["Panel/Button"]:setVisible( false )
		self._ui["Panel/stop"]:setVisible( false )
		self._partnerIcon:setVisible( false )
		self._isReadyIcon:setVisible( false ) -- 默认都不显示
		self._unReadyIcon:setVisible( false ) -- 默认都不显示
		self._forbiddenIcon:setVisible( false )
		self._followIcon:setVisible(true)

		local baseInfo = self._baseInfo
		self._head:setData( baseInfo )
		self._head:setQualityIcon()
		self._name:setString( baseInfo.name )

		self._class:setString(baseInfo:getDuty())

		self._lv_num:setString( string.format( L("lua_code_text_212"), baseInfo.lv ) )
		self:_refreshCombat(baseInfo.combatPower)
	else
		self._ui["Panel/Panel_withData"]:setVisible( false )
		self._ui["Panel/tips"]:setVisible( true )
		self._ui["Panel/Button"]:setVisible( true )
		self._ui["Panel/stop"]:setVisible( false )
	end
end

-- robotInfo = TeamRobotVo 数据结构
function ClassRef:setRobotInfo( robotInfo )
	self._data = nil
	self._baseInfo = nil
	self._partnerInfo = nil
	self._robotInfo = robotInfo
	if self._robotInfo then
		self._ui["Panel/Panel_withData"]:setVisible( true )
		self._ui["Panel/tips"]:setVisible( false )
		self._ui["Panel/Button"]:setVisible( false )
		self._ui["Panel/stop"]:setVisible( false )
		self._partnerIcon:setVisible( false )
		self._isReadyIcon:setVisible( false ) -- 默认都不显示
		self._unReadyIcon:setVisible( false ) -- 默认都不显示
		self._forbiddenIcon:setVisible( false )
		self._followIcon:setVisible(true)

		local baseInfo = self._robotInfo:getBaseInfo()
		self._head:setData( baseInfo )
		self._head:setQualityIcon()
		self._name:setString( baseInfo.name )

		self._class:setString(baseInfo:getDuty())

		self._lv_num:setString( string.format( L("lua_code_text_212"), baseInfo.lv ) )
		self:_refreshCombat(baseInfo.combatPower)
	else
		self._ui["Panel/Panel_withData"]:setVisible( false )
		self._ui["Panel/tips"]:setVisible( true )
		self._ui["Panel/Button"]:setVisible( true )
		self._ui["Panel/stop"]:setVisible( false )
	end
end

-- partnerInfo = Partner 数据结构
function ClassRef:setPartnerInfo( partnerInfo )
	self._data = nil
	self._baseInfo = nil
	self._robotInfo = nil
	self._partnerInfo = partnerInfo
	if self._partnerInfo then
		self._ui["Panel/Panel_withData"]:setVisible( true )
		self._ui["Panel/tips"]:setVisible( false )
		self._ui["Panel/Button"]:setVisible( false )
		self._ui["Panel/stop"]:setVisible( false )
		self._partnerIcon:setVisible( true )
		self._isReadyIcon:setVisible( false )
		self._unReadyIcon:setVisible( false )
		self._forbiddenIcon:setVisible( false )
		self._followIcon:setVisible(true)

		self._head:setIcon(self._partnerInfo:getIcon())
		self._head:setClassIcon(self._partnerInfo:getJobIcon())
		self._head:setQualityIcon( self._partnerInfo:getQuality() )

		self._name:setString( self._partnerInfo:getName() )
		self._class:setString( self._partnerInfo:getJobName() )
		self._lv_num:setString( string.format( L("lua_code_text_212"), self._partnerInfo:getLevel() ) )
		self:_refreshCombat(self._partnerInfo:getCombatPower())
	else
		self._ui["Panel/Panel_withData"]:setVisible( false )
		self._ui["Panel/tips"]:setVisible( true )
		self._ui["Panel/Button"]:setVisible( true )
		self._ui["Panel/stop"]:setVisible( false )
	end
end

function ClassRef:haveData()
	local data = self._data or self._partnerInfo or self._robotInfo or self._baseInfo
	return data ~= nil
end

function ClassRef:_refreshCombat(num)
	self._combat_num:setString(num)
	display.setNodesMiddle(self._dataPanel, nil, self._combat_icon, self._combat_num)
end

function ClassRef:setReadyStatus( readyStatus )
	if "yes" == readyStatus then --同意
		self._isReadyIcon:setVisible( true )
		self._unReadyIcon:setVisible( false )
	elseif "none" == readyStatus then --未确认
		self._isReadyIcon:setVisible( false )
		self._unReadyIcon:setVisible( true )
	else
		self._isReadyIcon:setVisible( false )
		self._unReadyIcon:setVisible( false )
	end
end

-- 刷新队长标志、之所以抽出来手动调用是因为 self._data 中的 isLeader 字段数据不准
function ClassRef:refreshTeamLeaderFlag( visible )
	self._isLeaderIcon:setVisible( visible )
	self._name:setColor( visible and cc.c3b( 254, 225, 131 ) or cc.c3b( 100, 195, 227 ) )
end

-- 刷新队长状态(暂离、战斗中)
function ClassRef:refreshLeaderStates()
	if self._data then
		if self._data:isLeader() then
			if self._data:isMe() then
				self._leaderTips:setVisible( false )
			else
				local stateStr = self._data:getActionStateStr()
				self._leaderTips:setVisible( "" ~= stateStr )
				self._leaderState:setString( stateStr )
			end
		else
			self._leaderTips:setVisible( false )
		end
	else
		self._leaderTips:setVisible( false )
	end
end

-- 禁止标志是否显示
-- 人数超限制
function ClassRef:setForbiddenIconVisible( visible )
	self._forbiddenIcon:setVisible( visible )
	if not self:haveData() then -- 空位？
		self._ui["Panel/Button"]:setVisible( not visible )
		self._ui["Panel/stop"]:setVisible( visible )
		self._ui["Panel/tips"]:setVisible( not visible )
	else
		self._ui["Panel/Button"]:setVisible( false )
		self._ui["Panel/stop"]:setVisible( false )
		self._ui["Panel/tips"]:setVisible( false )
	end
end

-- 禁止标志是否显示
-- 空位禁止
function ClassRef:refreshStopIconVisible()
	self._ui["Panel/Button"]:setVisible( false )
	self._ui["Panel/tips"]:setVisible( false )
	if not self:haveData() then -- 空位？
		self._ui["Panel/Panel_withData"]:setVisible( false )
		self._ui["Panel/stop"]:setVisible( true )
	else
		self._ui["Panel/Panel_withData"]:setVisible( true )
		self._ui["Panel/stop"]:setVisible( false )
	end
end

-- 是否是精英决斗场的上阵随从
function ClassRef:setIsOnBattlePartner(b)
	self._isOnBattlePartner = b
end

-- 修改tips
function ClassRef:setTips(str)
	if not str then return end
	self._ui["Panel/tips"]:setString(str)
end

function ClassRef:registerOnOperate( cb )
	self._onPanelHandler = cb
end

return ClassRef
